package com.ciquan.server.controller;

import com.ciquan.server.bean.SysMessage;
import com.ciquan.server.service.SysMessageService;
import com.ciquan.server.util.web.ParameterMap;
import com.ciquan.server.util.web.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by bjliwei2014 on 2015/3/10.
 */
@Controller
@RequestMapping("/api/sysMessage/")
public class ServiceSysMessageController extends AbstractController {

    @Resource
    private SysMessageService sysMessageService;

    @RequestMapping(value = "/list", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String list(HttpServletRequest request) {
        Result result = new Result();
        String userId = ServletRequestUtils.getStringParameter(request, "userId", "");
        int page = ServletRequestUtils.getIntParameter(request, "page", 1);
        int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 30);

        if (!isLogin(request)) {
            result.setSuccess(false);
            result.setStatusmsg("用户未登录");
            return result.toString();
        }

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("userId", userId);
        parameterMap.setStart((page - 1) * pageNum);
        parameterMap.setPageNum(pageNum);

        int count = sysMessageService.selectCount();
        List<SysMessage> sysMessages = sysMessageService.selectByUser(parameterMap);
        result.setSuccess(true);
        result.setTotal(count);
        result.setData(sysMessages);
        result.setStatusmsg("获取系统消息成功");
        return result.toString();
    }

    @RequestMapping(value = "/read", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String read(HttpServletRequest request) {
        Result result = new Result();
        String userId = ServletRequestUtils.getStringParameter(request, "userId", "");
        String messageId = ServletRequestUtils.getStringParameter(request, "messageId", "");

        if (!isLogin(request)) {
            result.setSuccess(false);
            result.setStatusmsg("用户未登录");
            return result.toString();
        }

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("userId", userId);
        parameterMap.put("messageId", messageId);
        sysMessageService.readMessage(parameterMap);
        result.setSuccess(true);
        result.setStatusmsg("消息已读");
        return result.toString();
    }

    @RequestMapping(value = "/unRead", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String unRead(HttpServletRequest request) {
        Result result = new Result();
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);

        if (!isLogin(request)) {
            result.setSuccess(false);
            result.setStatusmsg("用户未登录");
            return result.toString();
        }

        int unRead = sysMessageService.unReadCount(userId);
        result.setSuccess(true);
        result.setStatusmsg("获取未读消息成功");
        result.setData(unRead);
        return result.toString();
    }
}
